package com.box.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.box.entity.DbTheme;
import com.box.entity.DbThemeExample;
import com.box.entity.DbThemeExample.Criteria;
import com.box.mapper.DbThemeMapper;
import com.box.service.ThemeService;
import com.box.utils.LayuiResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class ThemeServiceImpl implements ThemeService {

	@Autowired
	private DbThemeMapper themeMapper;
	public LayuiResult selThemeList(Integer page, Integer limit) {
		// TODO Auto-generated method stub
		
		try {
			PageHelper.startPage(page, limit);
			DbThemeExample example =new DbThemeExample();
			example.setOrderByClause("updatetime desc");
			List<DbTheme> list = themeMapper.selectByExample(example);
			PageInfo<DbTheme> pageinfo=new PageInfo<DbTheme>(list);
			LayuiResult result=new LayuiResult();
			result.setCode(0);
			result.setCount(pageinfo.getTotal());
			result.setData(pageinfo.getList());
			result.setMsg("轮播图列表获取成功");
			return result;
		} catch (Exception e) {
			// TODO: handle exception
			LayuiResult result=new LayuiResult();
			result.setCode(500);
			result.setCount(null);
			result.setData(e);
			result.setMsg(e+"");
			return result;
		}
	
	}
	public Integer delThemeByid(String id) {
		// TODO Auto-generated method stub
		return themeMapper.deleteByPrimaryKey(Integer.parseInt(id));
	}
	public DbTheme selThemeByOne(String id) {
		// TODO Auto-generated method stub
		return themeMapper.selectByPrimaryKey(Integer.parseInt(id));
	}
	public Integer updateTheme(DbTheme theme) {
		// TODO Auto-generated method stub
		theme.setCreatetime(new Date());
		theme.setUpdatetime(new Date());
		return themeMapper.updateByPrimaryKeySelective(theme);
	}
	public Integer insertTheme(DbTheme theme) {
		// TODO Auto-generated method stub
		theme.setCreatetime(new Date());
		return themeMapper.insertSelective(theme);
	}
	public Integer deleteByAll(String ThemeStr) {
		// TODO Auto-generated method stub
		String[] ids = ThemeStr.split(",");
		Integer n=0;
		if(ids.length>0&&ids!=null){
			for (String string : ids) {
		n=		themeMapper.deleteByPrimaryKey(Integer.parseInt(string));
			}
			
		
		}
		System.out.println("批量删除"+n);
		return n;
	
	}
	public List<DbTheme> selTheme(Integer id) {
		// TODO Auto-generated method stub
		DbThemeExample example = new DbThemeExample();
		example.setOrderByClause("updatetime desc");
		Criteria criteria = example.createCriteria();
		criteria .andUppidEqualTo(id);
		return themeMapper.selectByExample(example);
	}
	public DbTheme selThemeByUppid(Integer uppid) {
		// TODO Auto-generated method stub
		DbThemeExample example = new DbThemeExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andUppidEqualTo(uppid);
		return themeMapper.selectByExample(example).get(0);
	}


}
